package com.huaguoguo.cyg.service.tl.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaguoguo.cyg.annotation.SkipIfKeyExists;
import com.huaguoguo.cyg.dao.tl.TlServerMapper;
import com.huaguoguo.cyg.entity.tl.dto.TlServerDTO;
import com.huaguoguo.cyg.entity.tl.vo.ServerInfoVO;
import com.huaguoguo.cyg.service.tl.data.ITlServerService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author huaguoguo
 * @since 2020-02-22
 */
@Service
public class TlServerServiceImpl extends ServiceImpl<TlServerMapper, TlServerDTO> implements ITlServerService {
    /**
     * 批量插入
     *
     * @param entityList ignore
     * @return ignore
     */
    @Override
    @SkipIfKeyExists(keys = {"areaName"})
    public boolean saveBatch(Collection<TlServerDTO> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    public int truncateTable(){
        return this.baseMapper.truncateTable();
    }

    @Override
    public List<ServerInfoVO> queryColumn(String... columns) {
        QueryWrapper<TlServerDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(columns);
        List<TlServerDTO> tlServerDTOList = baseMapper.selectList(queryWrapper);
        return tlServerDTOList.stream()
                .distinct()
                .map(server -> BeanUtil.toBean(server, ServerInfoVO.class)).collect(Collectors.toList());
    }

    @Override
    public List<ServerInfoVO> getAreaList(String serverName) {
        QueryWrapper<TlServerDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("area_name","area_id")
                .eq("server_name",serverName);
        List<TlServerDTO> tlServerDTOList = baseMapper.selectList(queryWrapper);
        return tlServerDTOList.stream()
                .map(server -> BeanUtil.toBean(server, ServerInfoVO.class)).collect(Collectors.toList());

    }
}
